<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>  
<c:set var="ctx" value="${pageContext.request.contextPath}"/> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<jsp:include page="../include.jsp"/>
<script type="text/javascript">

	var orgTree;
	$(function() {
		initTree();
		initCombotree();
		$('#deptName').textbox('textbox').css('background','#ccc');
		$('#cc').textbox('textbox').css('background','#ccc');
		$('#deptCode').textbox('textbox').css('background','#ccc');
		$('#sortCode').textbox('textbox').css('background','#ccc');
		$('#deptAlias').textbox('textbox').css('background','#ccc');
	});
	
	function initTree(){
		orgTree = $('#tt').tree({ 
	       lines:true,
	       checkbox: false,  
	       url: '${ctx}/org/getAllOrganization',  
	       formatter:function(node){
	    	   if(node.children.length){
		    	   return node.text +' <span style=\'color:blue\'>{' + node.children.length + '}</span>';
	    	   }
	    	   return node.text;
		   	},
		   	onContextMenu: function(e,node){
				e.preventDefault();
				$(this).tree('select',node.target);
				$('#mm').menu('show',{
					left: e.pageX,
					top: e.pageY
				});
			},
			onClick: function(node){
				loadOrg(node.id);
				//支持点击展开/折叠
            	$("#tt").tree(node.state === 'closed' ? 'expand' : 'collapse', node.target);
			},
			onLoadSuccess:function(node, data){
				if(data){				
					$(data).each(function(index, value) {
						if (this.state == 'open') {
							$('#tt').tree('collapseAll');
						}
					});
					expandRoot();
				} 
			}
	    });
	}
	
	function loadOrg(id){
		if(id){
			$('#ff').form('load','${ctx}/org/getOrgById?orgId='+id);
		}else{
			parent.$.messager.alert('错误', "初始化Form数据出错!", 'error');		
		}
	}
	
	//展开最顶层的 根节点
	function expandRoot(){
		//湖北中烟组织机构数据根节点ID值
		var rootId = '${orgRootId}';
		var root = orgTree.tree('find', rootId);
		orgTree.tree('expand', root.target);
	}
	
	function initCombotree(){
		$('#cc').combotree({    
			url: '${ctx}/org/getAllOrganization',    
		    required: false,
		    lines:true
		}); 
	}
	
	function collapse(){
		var node = $('#tt').tree('getSelected');
		$('#tt').tree('collapse',node.target);
	}
	function expand(){
		var node = $('#tt').tree('getSelected');
		$('#tt').tree('expand',node.target);
	}
	
	function collapseAll(){
		var node = $('#tt').tree('getSelected');
		$('#tt').tree('collapseAll',node.target);
	}
	function expandAll(){
		var node = $('#tt').tree('getSelected');
		$('#tt').tree('expandAll',node.target);
	}
	
	function add(){
		var node = $('#tt').tree('getSelected');
		addOrg(node.id);
	}
	
	
	function updateit(){
		var node = $('#tt').tree('getSelected');
		updateOrg(node.id);
	}
	
	function updateOrg(id){
		var url = '${ctx}/org/toOrgAdd?orgId='+id;
		parent.$.modalDialog({
			title:'修改组织机构',
			iconCls:'icon-save',
			href:url,
			width:'60%',
			height:'90%',
			resizable:true,
			collapsible:true,
			maximizable:true,
			buttons:[{
				text:'提交',
				iconCls:'icon-ok',
				handler:function() {
					parent.$.modalDialog.orgTree = orgTree;//因为添加成功之后，需要刷新这个tree，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function() {
					parent.$.messager.confirm('系统提示', '确定要关闭当前窗口?', function(r){
		                if (r){
		                	parent.$.modalDialog.handler.dialog('close');
		                }
		            });					
				}
			}]
		});
	}
	
	function removeit(){
		debugger;
		var node = $('#tt').tree('getSelected');
		var isLeaf =  $('#tt').tree('isLeaf', node.target); 
		if(!isLeaf){
			parent.$.messager.alert('提示', '当前节点是根节点，请先删除叶子节点', 'info');
			return;
    	}
		var id = node.id;		
		parent.$.messager.confirm('系统提示', '确定删除节点: '+node.text+' ', function(r){
            if (r){
            	$.ajax({
                    type: 'POST',
                    async: false,
                    dataType: "json",
                    url: '${ctx}/org/deleteOrg?orgId='+id,                    
                    success: function(result) {
                    	if(result.success == true) {
        					orgTree.tree('reload');
        					parent.$.messager.alert('提示', result.msg, 'info');
        				} else {
        					parent.$.messager.alert('错误', result.msg, 'error');		     						
        				}	 
                    }
                });
            }
        });		
	}
	
	//添加部门信息
	function addOrg(pId){
		var url = "${ctx}/org/toOrgAdd";
		if(typeof(pId) != "undefined"){
			url = '${ctx}/org/toOrgAdd?pId='+pId;
		}
		parent.$.modalDialog({
			title:'添加组织机构',
			iconCls:'icon-save',
			href:url,
			width:'60%',
			height:'90%',
			resizable:true,
			collapsible:true,
			maximizable:true,
			buttons:[{
				text:'提交',
				iconCls:'icon-ok',
				handler:function() {
					parent.$.modalDialog.orgTree = orgTree;//因为添加成功之后，需要刷新这个tree，所以先预定义好
					//parent.$.modalDialog.pId = id;
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function() {
					parent.$.messager.confirm('系统提示', '确定要关闭当前窗口?', function(r){
		                if (r){
		                	parent.$.modalDialog.handler.dialog('close');
		                }
		            });					
				}
			}]
		});
	}

</script>
</head>
<body>
	<div class="easyui-layout" data-options="fit:true">
	    <div data-options="region:'west',split:false,title:''" style="width:40%;padding:10px;">
	    	<ul id="tt"></ul> 
	    </div>
		<div data-options="region:'center',border:true">	
			<form id="ff" method="post">			
				<div style="margin-bottom:15px;padding-top:70px;padding-left:30px">
	                <input id="deptName" class="easyui-textbox" value="" name="deptName" style="width:70%" data-options="label:'组织机构名称:',labelWidth:120">
	            </div>
	            <div style="margin-bottom:15px;padding-top:10px;padding-left:30px">
	                <input id="cc" name="parentId" value="" style="width:70%" data-options="label:'上级部门:',labelWidth:120,readonly:true">
	            </div>
	            <div style="margin-bottom:15px;padding-top:10px;padding-left:30px">
	                <input id="deptCode" class="easyui-textbox" value="" name="deptCode" style="width:70%" data-options="label:'机构编码:',labelWidth:120,readonly:true">
	            </div>
	            <div style="margin-bottom:15px;padding-top:10px;padding-left:30px">
	                <input id="sortCode" class="easyui-textbox" value="" name="sortCode" style="width:70%" data-options="label:'排序:',labelWidth:120">
	            </div>
	            <div style="margin-bottom:15px;padding-top:10px;padding-left:30px">
	                <input id="deptAlias" class="easyui-textbox" value="" name="deptAlias" style="width:70%" data-options="label:'行政组织简称:',labelWidth:120">
	            </div>
			 </form>		
		 </div>
	</div>
	
	<div id="mm" class="easyui-menu" style="width:120px;">
		<div onclick="add();" data-options="iconCls:'icon-add'">添加</div>
		<div onclick="updateit();" data-options="iconCls:'icon-edit'">修改</div>
		<div onclick="removeit();" data-options="iconCls:'icon-remove'">删除</div>
		<div class="menu-sep"></div>
		<div onclick="collapseAll()" data-options="iconCls:'icon-standard-arrow-in'">折叠所有</div>
		<div onclick="expandAll()" data-options="iconCls:'icon-standard-arrow-out'">展开所有</div>
		<div onclick="expand()" data-options="iconCls:'icon-metro-expand2'">展开当前</div>
		<div onclick="collapse()" data-options="iconCls:'icon-metro-contract2'">折叠当前</div>		
	</div>
</body>
</html>